package com.taotao.content.service.impl;

import com.taotao.common.pojo.Const;
import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.IContentCategoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangxiaolin@zhonhajt.com
 * @Title: 内容分类接口业务实现
 * @Package PACKAGE_NAME
 * @Description:
 * @date 2018/10/18 21:06
 */
@Service
public class ContentCategoryServiceImpl implements IContentCategoryService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EasyUITreeNode> getContentCategoryList(long parentId) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> categoryList = contentCategoryMapper.selectByExample(example);
        List<EasyUITreeNode> treeNodeList = new ArrayList<>();

        if (categoryList.size() >0) {
            for(TbContentCategory category : categoryList) {
                EasyUITreeNode node = new EasyUITreeNode();
                node.setId(category.getId());
                node.setText(category.getName());
                node.setState(category.getIsParent() ? "closed" : "open");
                treeNodeList.add(node);
            }
        }
        return treeNodeList;
    }

    @Override
    public TaotaoResult addContentCategory(long parentId, String name) {
        TbContentCategory category = new TbContentCategory();
        category.setParentId(parentId);
        category.setName(name);
        //状态。可选值:1(正常),2(删除)'
        category.setStatus(1);
        category.setSortOrder(1);
        category.setIsParent(false);
        category.setCreated(new Date());
        category.setUpdated(new Date());

        //插入数据库
        contentCategoryMapper.insert(category);

        //查询当前节点父节点
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        TbContentCategory parentCategory = contentCategoryMapper.selectByPrimaryKey(parentId);

        //判断父节点状态
        if (parentCategory != null) {
            //如果父节点为叶子节点应该改为父节点
            if (!parentCategory.getIsParent()) {
                parentCategory.setIsParent(true);
                contentCategoryMapper.updateByPrimaryKey(parentCategory);
            }
        }
        //返回结果
        return TaotaoResult.ok(category);
    }

    @Override
    public TaotaoResult updateContentCategory(long id, String name) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        contentCategory.setId(id);
        contentCategory.setName(name);

        int count = contentCategoryMapper.updateByPrimaryKey(contentCategory);
        if (count > 0) {
            return TaotaoResult.ok();
        }
        return TaotaoResult.error("更新失败");
    }

    @Override
    public TaotaoResult deleteContentCategory(long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);

        TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        if (contentCategory.getParentId().equals(Const.rootParentId)) {
            return TaotaoResult.error("分类根节点不允许删除");
        }

        //当前节点不是父节点，直接删除
        if (!contentCategory.getIsParent()) {
            contentCategoryMapper.deleteByExample(example);

            TbContentCategoryExample parentExample = new TbContentCategoryExample();
            TbContentCategoryExample.Criteria parentCriteria = example.createCriteria();
            parentCriteria.andParentIdEqualTo(contentCategory.getParentId());
            List<TbContentCategory> categoryList = contentCategoryMapper.selectByExample(example);

            if (categoryList.size() == 0) {
                TbContentCategory parent = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
                parent.setId(contentCategory.getParentId());
                parent.setIsParent(false);
                //2、判断父节点下是否还有子节点，如果没有需要把父节点的isparent改为false
                contentCategoryMapper.updateByPrimaryKey(parent);
                return TaotaoResult.ok();
            }else {
                return TaotaoResult.ok();
            }
        }

        //当前为普通父节点，递归删除其子节点
        example.clear();    //清空查询条件
        example.createCriteria().andParentIdEqualTo(id);
        List<TbContentCategory> childCategoryList = contentCategoryMapper.selectByExample(example);
//        for (TbContentCategory category : childCategoryList) {
//
//        }
        return null;
    }
}
